
package org.sopt.dev.ImgKnight.frontier.Database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

public class FpDocsDAO {
	
	private Logger exceptionLogger = null;
	
	public static Connection getConnection() {
		try {
			Class.forName("org.sqlite.JDBC");
			return DriverManager.getConnection("jdbc:sqlite:fpdocs.db");
		} catch (ClassNotFoundException e) {
		} catch (SQLException e) {
		}
		return null;
	}
	
	public FpDocsDAO(boolean isCleanStart) {
		exceptionLogger = Logger.getLogger("frontier.exception");
		
		Connection conn = null;
		Statement stat = null;
		try {
			conn = getConnection();
			if( conn != null ) {
				stat = conn.createStatement();
				if( isCleanStart )
					stat.executeUpdate("drop table if exists fpdocs;");
				stat.executeUpdate("create table if not exists fpdocs(RabinHashCode int primary key);");
			}
		} catch (Exception e) {
			exceptionLogger.error("Unexpected exception: "+e);
		} finally {
			if( stat != null ) try{ stat.close(); } catch(Exception e){}
			if( conn != null ) try{ conn.close(); } catch(Exception e){}
		}
	}
	
	public synchronized boolean selectFpDocs(long urlHashValue) {
		Connection conn = null;
		PreparedStatement stat = null;
		ResultSet rs = null;
		boolean result = false;
		try {
			conn = getConnection();
			if( conn != null ) {
				stat = conn.prepareStatement("select RabinHashCode from fpdocs where RabinHashCode=?;");
				stat.setLong(1, urlHashValue);
				rs = stat.executeQuery();
				if( rs.next() ) {
					if( urlHashValue == rs.getLong("RabinHashCode") )
						result = true;
				}
			}
		} catch (Exception e) {
			exceptionLogger.error("Unexpected exception: "+e);
		} finally {
			if( rs != null ) try{ rs.close(); } catch(Exception e){}
			if( stat != null ) try{ stat.close(); } catch(Exception e){}
			if( conn != null ) try{ conn.close(); } catch(Exception e){}
		}
		return result;
	}
	
	public synchronized boolean insertFpDocs(Long [] fpdocs) {
		Connection conn = null;
		PreparedStatement stat = null;
		try {
			conn = getConnection();
			if( conn != null ) {
				stat = conn.prepareStatement("insert into fpdocs values(?);");
				for(Long rabinHashCode : fpdocs) {
					stat.setLong(1, rabinHashCode.longValue());
					stat.addBatch();
				}
				conn.setAutoCommit(false);
				stat.executeBatch();
				conn.setAutoCommit(true);
				return true;
			}
		} catch (Exception e) {
			exceptionLogger.error("Fpdocs flush fail: "+e);
		} finally {
			if( stat != null ) try{ stat.close(); } catch(Exception e){}
			if( conn != null ) try{ conn.close(); } catch(Exception e){}
		}
		return false;
	}
	
	public synchronized int selectFpDocsSize() {
		Connection conn = null;
		PreparedStatement stat = null;
		ResultSet rs = null;
		int size = -1;
		try {
			conn = getConnection();
			if( conn != null ) {
				stat = conn.prepareStatement("select count(RabinHashCode) as size from fpdocs;");
				rs = stat.executeQuery();
				if( rs != null ) {
					if( rs.next() )
						size = rs.getInt("size");
				}
			}
		} catch (Exception e) {
			exceptionLogger.error("Unexpected exception: "+e);
		} finally {
			if( rs != null ) try{ rs.close(); } catch(Exception e){}
			if( stat != null ) try{ stat.close(); } catch(Exception e){}
			if( conn != null ) try{ conn.close(); } catch(Exception e){}
		}
		return size;
	}
	
}
